package daylight;

import java.io.*;
import java.util.ArrayList;

public class 对象流 {//对象流是字节流

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("DAY12\\aa.txt"));
        User user1 = new User("zhang", "024831");
        User user2 = new User("wang", "024831");
        User user3 = new User("li", "024831");
        oos.writeObject(user1);
        //反序列化流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("DAY12\\aa.txt"));
        User userA = (User) ois.readObject();
        System.out.println(userA);
        while (true){
            try {
                System.out.println(ois.readObject());
            } catch (IOException e) {
                e.printStackTrace();
                break;//捕获异常结束循环
            }
        }
        ArrayList<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        oos.writeObject(users);
        System.out.println(ois.readObject());
    }
}
class User implements Serializable{//要想被写在本地文件中必须实现序列化接口否则NotSerializableException
    private String name;
    private String password;
    private static final long SerialVersionUIDAdder = 500L;
    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}